package com.qq.afterSaleSystem.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qq.afterSaleSystem.entity.Apply;
import com.qq.afterSaleSystem.entity.ShopMaintain;
import com.qq.afterSaleSystem.entity.MaintainMoney;
import com.qq.afterSaleSystem.entity.Part;
import com.qq.afterSaleSystem.service.serviceImpl.ShopMaintainServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/shopmaintain")
public class ShopMaintainController {
    @Autowired
    ShopMaintainServiceImpl shopMaintainServiceImpl;

    @Autowired
    HttpServletRequest request;

    @RequestMapping("/doingmaintain")
    public String getAll(Model m, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,4);
        List<ShopMaintain> employees = shopMaintainServiceImpl.getAll1();
        System.out.println("维修信息为"+employees);
        PageInfo<ShopMaintain> employeesPageInfo = new PageInfo<>(employees);
        m.addAttribute("PageInfo",employeesPageInfo);

        //获取维修中，每种维修类型的数量
//
        return "lzz/tables";
    }

    @RequestMapping("getmaintainnum1")
    @ResponseBody
    public List<ShopMaintain> getNumOfMaintainTypeAndDo(){
        List<ShopMaintain> nummaintain = shopMaintainServiceImpl.getNumOfMaintainTypeAndDo();
        System.out.println("维修类型数量结果"+nummaintain);
        return nummaintain;
    }

    @RequestMapping("/page")
    public String page(){
        return "lzz/tables";
    }

    @RequestMapping("maintainperson")
    @ResponseBody
    public List<ShopMaintain> getNumOfMaintainPerson(){
        List<ShopMaintain> numMaintainPerson = shopMaintainServiceImpl.getNumOfMaintainPerson();
        return numMaintainPerson;
    }

    @RequestMapping("/addShopMaintainPage")
    public String addShopMaintainPage(){
        return "lzz/addShopMaintain";
    }

    @RequestMapping("/addShopMaintain")
    @ResponseBody
    public int addShopMaintain(ShopMaintain cmt){
        if(cmt.getCarNumber()==null||cmt.getMaintainType()==null||cmt.getCarType()==null||cmt.getPlateNumber()==null||
        cmt.getColor()==null||cmt.getVinNum()==null||cmt.getEngineNumber()==null||cmt.getPerson()==null||cmt.getJobNum()==null){
            return -1;
        }else {
            Date nowdate = new Date();
            cmt.setGetTime(nowdate);
            cmt.setState(1);
            System.out.println();
            int i = shopMaintainServiceImpl.addShopMaintain(cmt);
            return i;
        }
    }

    @RequestMapping("/updateShopMaintainById")
    public String shopMaintainById(Model m,Integer id){
        ShopMaintain cmt = shopMaintainServiceImpl.shopMaintainById(id);
        m.addAttribute("cmt",cmt);
        return "lzz/updateShopMaintain";
    }

    @RequestMapping("/updateShopMaintain")
    @ResponseBody
    public int updateShopMaintain(ShopMaintain cmt, String date) throws ParseException {
       /* if(null!=date){
            cmt.setState(0);

        }else {
            cmt.setState(1);
        }*/
        if(null!=date){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            cmt.setOverTime(format.parse(date));
        }
        int i = shopMaintainServiceImpl.updateShopMaintain(cmt);
        return i;
    }

    @RequestMapping("/deleteShopMaintain")
    @ResponseBody
    public int deleteShopMaintain(Integer id){
        int i = shopMaintainServiceImpl.deleteShopMaintain(id);
        return i;
    }

    @RequestMapping("/addApplyById")
    public String addApplyById(Model m,Integer id){
        ShopMaintain cmt = shopMaintainServiceImpl.shopMaintainById(id);
        m.addAttribute("cmt",cmt);
        String type = cmt.getCarType();
        System.out.println("--------type--------"+type);
        List<Part> parts = shopMaintainServiceImpl.getPart();
        List<Part> part1=new ArrayList<>();
        for (Part part : parts) {

            if (part.getPartVar().equals(cmt.getCarType())){
                part1.add(part);
                System.out.println("-----part---------"+part);
            }
        }
        m.addAttribute("part",part1);
        return "lzz/addApply";
    }

    @RequestMapping("/insertApply")
    @ResponseBody
    public int insertApply(Apply ap){
        Date date = new Date();
        ap.setTime(date);
        ap.setState(0);
        int i = shopMaintainServiceImpl.insertApply(ap);
        return i;
    }

    @RequestMapping("/getApplyByCarId")
    public String getApplyByCarId(Model m,Integer id){
        List<Apply> applyByCarId = shopMaintainServiceImpl.getApplyByCarId(id);
        m.addAttribute("applyByCarId",applyByCarId);
        return "lzz/applyOfShopList";
    }

    @RequestMapping("/getAllApply")
    public String getAllApply(Model m){
        List<Apply> allApply = shopMaintainServiceImpl.getAllApply();
        m.addAttribute("allApply",allApply);
        return "lzz/allApply";
    }

    @RequestMapping("/deleteApply")
    @ResponseBody
    public int deleteApply(Integer id){
        int i = shopMaintainServiceImpl.deleteApply(id);
        return i;
    }

    @RequestMapping("/updateApplyStateTo1")
    @ResponseBody
    public int updateApplyStateTo1(Integer id,String partsName,Integer carId){
        int i = shopMaintainServiceImpl.updateApplyStateTo1(id,partsName);
        if(i!=0){
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String date1 = sdf.format(date);
            int i1 = shopMaintainServiceImpl.insertMaintainMoney(carId, partsName, date1);
        }
        return i;
    }

    @RequestMapping("/getAllApplyStateIs1")
    public String getAllApplyStateIs1(Model m){
        List<Apply> allApply = shopMaintainServiceImpl.getAllApplyStateIs1();
        m.addAttribute("allApply",allApply);
        return "lzz/allApplyStateIs1";
    }

    @RequestMapping("/getMaintainMoneyByCarId")
    public String getMaintainMoneyByCarId(Model m,Integer id){
        List<MaintainMoney> moneyByCarId = shopMaintainServiceImpl.getMaintainMoneyByCarId(id);
        m.addAttribute("moneyByCarId",moneyByCarId);
        return "lzz/moneyByCarId";
    }

    @RequestMapping("/getMoneyById")
    public String getMoneyById(Model m,Integer id){
        MaintainMoney moneyById = shopMaintainServiceImpl.getMoneyById(id);
        m.addAttribute("moneyById",moneyById);
        return "lzz/updateMoneyById";
    }

    @RequestMapping("/updateMaintainMoney")
    @ResponseBody
    public int updateMaintainMoney(MaintainMoney money){
        int i = shopMaintainServiceImpl.updateMoneyById(money);
        return i;
    }
}
